﻿package core.drawing
{
    import core.enum.*;
    import flash.display.*;
    import flash.events.*;

    final public class DodgeTool extends ShapeTool
    {
        var source:BitmapData;
        var px:uint;
        var f:Number;
        var tf:Number;
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var alpha:int;
        var a:int;
        var r:int;
        var g:int;
        var b:int;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;

        public function DodgeTool(param1:Tuyitu)
        {
            super(param1, ToolType.Dodge);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                this.Prepare(Action.DodgeTool, layer.activeBitmap);
                if (target != null)
                {
                    this.source = layer.bitmap.bitmapData.clone();
                }
                if (!event.shiftKey)
                {
                    var _loc_2:int = -1;
                    y = -1;
                    x = _loc_2;
                }
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(UpdateReferences:int, UpdateReferences:int) : void
        {
            this.n = 0;
            this.sy = UpdateReferences - int(shape.height * 0.5);
            this.cy = this.sy + shape.height;
            while (this.sy < this.cy)
            {
                
                this.sx = UpdateReferences - int(shape.width * 0.5);
                this.cx = this.sx + shape.width;
                while (this.sx < this.cx)
                {
                    
                    this.alpha = shape.data[this.n];
                    if (this.alpha > 0 && (selection == null || selection.IsSelected(this.sx + offset.x, this.sy + offset.y)))
                    {
                        this.px = this.source.getPixel32(this.sx, this.sy);
                        this.a = this.px >>> 24;
                        if (this.a > 0)
                        {
                            this.r = this.px >>> 16 & 255;
                            this.g = this.px >>> 8 & 255;
                            this.b = this.px & 255;
                            this.f = this.r + this.g + this.b;
                            switch(options.range)
                            {
                                case 2:
                                {
                                    this.f = this.f / 3 * options.exposure;
                                    this.r = this.r + this.f;
                                    this.g = this.g + this.f;
                                    this.b = this.b + this.f;
                                    break;
                                }
                                case 1:
                                {
                                    this.f = this.f - 384 < 0 ? (384 - (this.f - 384) * -1) : (384 - (this.f - 384));
                                    this.f = this.f / 10 * options.exposure;
                                    this.r = this.r + this.f;
                                    this.g = this.g + this.f;
                                    this.b = this.b + this.f;
                                    break;
                                }
                                case 0:
                                {
                                    this.f = (765 - this.f) / 3 * (options.exposure * 0.5);
                                    this.r = this.r + this.f;
                                    this.g = this.g + this.f;
                                    this.b = this.b + this.f;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            if (this.r > 255)
                            {
                                this.r = 255;
                            }
                            if (this.g > 255)
                            {
                                this.g = 255;
                            }
                            if (this.b > 255)
                            {
                                this.b = 255;
                            }
                            if (this.alpha < 255)
                            {
                                this.px = target.getPixel32(this.sx, this.sy);
                                this.ta = this.px >>> 24 & 255;
                                this.tr = this.px >>> 16 & 255;
                                this.tg = this.px >>> 8 & 255;
                                this.tb = this.px & 255;
                                this.f = this.alpha / 255;
                                this.tf = 1 - this.f;
                                this.tr = this.r * this.f + this.tr * this.tf;
                                this.tg = this.g * this.f + this.tg * this.tf;
                                this.tb = this.b * this.f + this.tb * this.tf;
                                target.setPixel32(this.sx, this.sy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                            }
                            else
                            {
                                target.setPixel(this.sx, this.sy, this.a << 24 | this.r << 16 | this.g << 8 | this.b);
                            }
                        }
                    }
                    var _loc_3:String = this;
                    var _loc_4:* = this.n + 1;
                    _loc_3.n = _loc_4;
                    var _loc_3:String = this;
                    var _loc_4:* = this.sx + 1;
                    _loc_3.sx = _loc_4;
                }
                var _loc_3:String = this;
                var _loc_4:* = this.sy + 1;
                _loc_3.sy = _loc_4;
            }
            return;
        }// end function

    }
}
